package com.yanqu.road.server.cmd.passRoad;

import com.yanqu.road.entity.config.minigame.MiniGameConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.pass.RoadProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.system.MiniGameConfigMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = Protocol.S_PASS_ROAD_COST_SLIVER_TEST, desc = "引导获得奖励")
public class PassRoadCostSliverTestCmd extends AbstractCommand {
    private int adId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RoadProto.PassRoadCostSilverReq reqMsg = RoadProto.PassRoadCostSilverReq.parseFrom(packet.getBytes());
        adId = reqMsg.getAdId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        if (!MiniGameConfigMgr.hasMiniGame()) {
            return;
        }
        BagModule bagModule = player.getModule(BagModule.class);
        RoadProto.PassRoadCostSilverResp.Builder respMsg = RoadProto.PassRoadCostSilverResp.newBuilder();
        respMsg.setCostSilver(0);
        respMsg.setDamagePow("");
        respMsg.setDamage("");
        int goodsId = 119999;
        UserBagItem bagItem = bagModule.getUserBagItem(goodsId);
        UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
        BigInteger earnSpeed = userHistoryAttribute.getEarnSpeed();
        if (earnSpeed == null) {
            earnSpeed = BigInteger.ZERO;
        }
        if ((bagItem != null && bagItem.getNum() > 0) || earnSpeed.compareTo(BigInteger.ZERO) > 0) {

        } else {
            bagModule.addBagItem(goodsId, 1, eLogMoneyType.System);
            Property reward = new Property(GameConfig.GAME_MONEY_INGOTS, 50);
            respMsg.setDamage(PropertyHelper.parsePropertyToString(reward));
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.System, eLogMoneyType.System);
            //广告福利
            try {
                sendAdReward(player, adId);
            }catch (Exception e){
                log.error("send mini game ad reward error:", e);
            }
        }
        player.sendPacket(ClientProtocol.U_PASS_ROAD_COST_SLIVER_TEST, respMsg);
    }

    private void sendAdReward(GamePlayer player, int miniGameAdId){
        if(miniGameAdId > 0 && ChannelConfig.MINI_GAME_AD_REWARD_SWITCH == 1) {
            MiniGameConfig miniGameConfig = MiniGameConfigMgr.getMiniGameConfig(miniGameAdId);
            if(null == miniGameConfig){
                return;
            }
            AttributeModule attributeModule = player.getModule(AttributeModule.class);
            long adReward = attributeModule.getAttribute(ePlayerAttrType.MiniGameAdReward);
            if(adReward > 0){//已经领取过
                return;
            }
            String language = player.getLanguage();
            String mailTitle = MultipleLanguageMgr.getContent(MailManager.MINI_GAME_AD_REWARD_TITLE, language);
            String mailContent = MultipleLanguageMgr.getContent(MailManager.MINI_GAME_AD_REWARD_CONTENT, language);
            attributeModule.setAttribute(ePlayerAttrType.MiniGameAdReward, 1);
            MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), GameConfig.MINI_GAME_AD_REWARD, mailContent, mailTitle);
        }
    }
}
